using System;
using System.Collections.Generic;
using System.Text;

namespace GetTogether.Studio.Schedule
{
    public class ScheduleHistoryList : GetTogether.ObjectBase.ListBase<ScheduleHistoryList.ScheduleHistory>
    {
        public ScheduleHistoryList()
        {

        }

        public static string GetHistoryPath(string scheduleName)
        {
            return System.IO.Path.Combine(Schedule.ScheduleTask.GetSettingsPathPublic(), string.Concat(scheduleName, "(History)"));
        }

        public static void Save(ScheduleTask schedule, string error, string response)
        {
            string dir = GetHistoryPath(schedule.Name);
            if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
            string file = string.Concat(dir, "\\", schedule.ActionOn.ToString("yyyyMMddHHmmss"));
            ScheduleHistory sh = new ScheduleHistory();
            sh.ActionOn = schedule.ActionOn;
            sh.Error = error;
            if (!string.IsNullOrEmpty(response))
            {
                string[] responseInfo = response.Split(new string[] { "=============================================================================" }, StringSplitOptions.None);
                if (responseInfo.Length == 3)
                {
                    sh.Report = responseInfo[0];
                    sh.RequestInfo = responseInfo[1];
                    sh.ResultInfo = responseInfo[2];
                }
                else
                {
                    sh.ResultInfo = response;
                }
            }
            System.IO.File.WriteAllText(file, GetTogether.Utility.SerializationHelper.SerializeToXml(sh));
        }

        public static ScheduleHistoryList GetHistorys(string scheduleName)
        {
            string dir = GetHistoryPath(scheduleName);
            if (System.IO.Directory.Exists(dir))
            {
                ScheduleHistoryList shs = new ScheduleHistoryList();
                string[] files = System.IO.Directory.GetFiles(dir);
                foreach (string file in files)
                {
                    try
                    {
                        if (file.ToLower().EndsWith(".history")) continue;
                        ScheduleHistory sh = GetTogether.Utility.SerializationHelper.FromXml<ScheduleHistory>(System.IO.File.ReadAllText(file));
                        System.IO.FileInfo fi = new System.IO.FileInfo(file);
                        sh.CreateOn = fi.CreationTime;
                        sh.File = file;
                        shs.Add(sh);
                    }
                    catch (Exception ex)
                    {
                        GetTogether.Studio.Log.Error(ex);
                        System.IO.File.Delete(file);
                    }
                }
                shs.SortBy("CreateOn", false);
                if (shs.Count > 20)
                {
                    for (int i = 9; i < shs.Count; i++)
                    {
                        System.IO.File.Delete(shs[i].File);
                    }
                }
                return shs;
            }
            return null;
        }

        public class ScheduleHistory
        {
            private string _Error;

            public string Error
            {
                get { return _Error; }
                set { _Error = value; }
            }

            private DateTime _ActionOn;

            public DateTime ActionOn
            {
                get { return _ActionOn; }
                set { _ActionOn = value; }
            }

            private DateTime _CreateOn;

            public DateTime CreateOn
            {
                get { return _CreateOn; }
                set { _CreateOn = value; }
            }

            private string _File;

            public string File
            {
                get { return _File; }
                set { _File = value; }
            }

            private string _Report;

            public string Report
            {
                get { return _Report; }
                set { _Report = value; }
            }

            private string _RequestInfo;

            public string RequestInfo
            {
                get { return _RequestInfo; }
                set { _RequestInfo = value; }
            }

            private string _ResultInfo;

            public string ResultInfo
            {
                get { return _ResultInfo; }
                set { _ResultInfo = value; }
            }

            public ScheduleHistory()
            {

            }
        }
    }
}
